package com.central.apps.handler.MaterialInventory;

import com.central.app.common.base.BaseHandler;
import com.central.app.service.HandlerService;
import com.central.apps.common.util.DateUtiles;
import com.central.apps.util.StringUtil;
import com.central.bizcommon.model.flow.FlowNode;
import com.central.bizcommon.model.flow.FlowOpinion;
import com.central.common.exception.service.ServiceException;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.common.recycler.Recycler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//报废申请
@Component
@Slf4j
public class ScrapApplicationHandler extends BaseHandler {
    @Autowired
    private HandlerService handlerService;

    @Override
    public String beforeExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op) throws Exception {
        if (flowNode.getId().equals("submit")) {
            List<Map<String, Object>> bySql = handlerService.findBySql("select scraption,warehouse from gq_scraplication where id=" + id);
            Map<String, Object> map = bySql.get(0);
            String scraption = MapUtils.getString(map, "scraption");
            String warehouse = MapUtils.getString(map, "warehouse");
            List<Map<String, Object>> bySql1 = handlerService.findBySql("select * from gq_Listscrapped where scraption ='" + scraption + "'");
            if (CollectionUtils.isEmpty(bySql1)) {
                return "请添加报废物资列表信息！";
            } else {
                for (int i = 0; i < bySql1.size(); i++) {
                    Map<String, Object> map1 = bySql1.get(i);
                    String id1 = MapUtils.getString(map1, "id");
                    Float inquantity = MapUtils.getFloat(map1, "Inquantity");//当前报废表的可用数量100
                    Float quantity = MapUtils.getFloat(map1, "quantity");//当前报废表数量40
                    String matcode = MapUtils.getString(map1, "matcode");
                    String batch = MapUtils.getString(map1, "batch");
//                    List<Map<String, Object>> bySql3 = handlerService.findBySql("select t.* from gq_Listscrapped t left join gq_scraplication a on t.scraption=a.scraption where t.matcode='" + matcode + "' and t.batch='" + batch + "' and a.status!='已批准'");
//                    if (CollectionUtils.isNotEmpty(bySql3)){
//                        for (int z=0;z<bySql3.size();z++){
//                            Map<String, Object> map2 = bySql3.get(z);
//                            Float quantity1 = MapUtils.getFloat(map2, "quantity");
//                            quantity3=quantity1+quantity3;//报废子表的报废总数量90
//                        }
//                    }
                    if (quantity==0){
                        int y = i+1;
                        return "报废物料第" + y + "条数据中报废数量为0，请修改报废数量！";
                    }
                    List<Map<String, Object>> bySql2 = handlerService.findBySql("select t.* from gq_Listscrapped t left join gq_scraplication a on t.scraption=a.scraption where t.matcode='" + matcode + "' and t.batch='" + batch + "' and a.status!='新建' and  a.status!='已作废' and a.status!='已批准'");
                    float quantity2 = 0;
                    if (CollectionUtils.isNotEmpty(bySql2)) {
                        for (int j = 0; j < bySql2.size(); j++) {
                            Map<String, Object> map2 = bySql2.get(j);
                            //Float inquantity1 = MapUtils.getFloat(map2, "Inquantity");//报废子表的可用数量
                            Float quantity1 = MapUtils.getFloat(map2, "quantity");//报废子表的报废数量
                            quantity2 = quantity2 + quantity1;//报废子表的报废总数量
                        }
                        quantity = quantity + quantity2;
                    }
                        List<Map<String, Object>> bySql4 = handlerService.findBySql("select * from gq_invbalances where itemnum='" + matcode + "' and batchnum='" + batch + "' and storenum ='" + warehouse + "'");
                        Float avblbalance = (float) 0;
                        if (CollectionUtils.isNotEmpty(bySql4)) {
                            Map<String, Object> map2 = bySql4.get(0);
                            avblbalance = MapUtils.getFloat(map2, "avblbalance");
                        }
//                        List<Map<String, Object>> bySql3 = handlerService.findBySql("select t.* from gq_Listscrapped t left join gq_scraplication a on t.scraption=a.scraption where t.matcode='" + matcode + "' and t.batch='" + batch + "' and a.status!='已批准' and a.status!='已作废'");
//                        if (CollectionUtils.isNotEmpty(bySql3)){
//                            List<Map<String, Object>> bySql5 = handlerService.findBySql("select t.* from gq_Listscrapped t left join gq_scraplication a on t.scraption=a.scraption where t.matcode='" + matcode + "' and t.batch='" + batch + "' and a.status!='新建' and  a.status!='已作废' and  a.status!='已批准'");
//                            if (CollectionUtils.isNotEmpty(bySql5)){
//                                float quantity3=0;
//                                for (int z=0;z<bySql5.size();z++){
//                                    Map<String, Object> map2 = bySql5.get(z);
//                                    Float quantity1 = MapUtils.getFloat(map2, "quantity");
//                                    quantity3=quantity3+quantity1;
//                                }
//                                avblbalance=avblbalance-quantity3;
//                                Float quantity4 = MapUtils.getFloat(map1, "quantity");//当前报废表数量40
//                                if (avblbalance.compareTo(quantity4)<0){
//                                    int y = i + 1;
//                                    return "报废物料第"+y+"条数据中部分物料被人占用，当前库存剩余"+avblbalance+"，请重新选择物料！";
//                                }
//                            }else if (CollectionUtils.isEmpty(bySql5)){
                        Float quantity4 = MapUtils.getFloat(map1, "quantity");//当前报废表数量40
                        if (avblbalance.compareTo(quantity) < 0) {
                            int y = i + 1;
                            avblbalance = avblbalance - quantity2;
                            return "报废物料第" + y + "条数据中部分物料被人占用，当前库存剩余" + avblbalance + "，请重新选择物料！";
                        }

                }
//                else {
//                            if (avblbalance.compareTo(quantity)<0) {
//                            /*
//90
//                    int y = i+1;
//                    msg ="&#x8c03;&#x62e8;&#x7269;&#x6599;&#x7b2c;"+y+"&#x6761;&#x6570;&#x636e;&#x4e2d;&#x7269;&#x6599;&#x73b0;&#x6709;&#x53ef;&#x7528;&#x4f59;&#x91cf;&#x4e3a;"+avblbalance+",&#x4e0e;&#x8be5;&#x8c03;&#x62e8;&#x8868;&#x4e2d;&#x53ef;&#x7528;&#x4f59;&#x91cf;&#x4e0d;&#x7b49;&#xff0c;&#x8bf7;&#x91cd;&#x65b0;&#x9009;&#x62e9;&#x7269;&#x6599;";
//*/
//                                int y = i + 1;
//                                float avblbalance1=avblbalance-quantity2;
//                                return "报废物料第"+y+"条数据中部分物料被人占用，当前库存剩余"+avblbalance1+"，请重新选择物料！";
//                                //return "报废物料第" + y + "条数据中物料库存不足,请重新选择物料！";
//                            }
//                        }

            }




        }
        return super.beforeExcuteFlow(app, id, flowNode, op);
    }

//    @Override
//    public void beforSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
//        if ("gq_Listscrapped".equals(objName)){
//            String scraption = MapUtils.getString(data, "scraption");
//            List<Map<String, Object>> bySql1 = handlerService.findBySql("select warehouse from gq_scraplication where scraption='" + scraption + "'");
//            Map<String, Object> map1 = bySql1.get(0);
//            String warehouse = MapUtils.getString(map1, "warehouse");
//            //String subjectno = MapUtils.getString(map1, "subjectno");
////            List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_Listscrapped where scraption ='" + scraption + "'");
////            if (CollectionUtils.isNotEmpty(bySql)){
////                for (int i=0;i<bySql.size();i++){
////                    Map<String, Object> map = bySql.get(i);
////                    String matcode = MapUtils.getString(map, "matcode");
////                    String batch = MapUtils.getString(map, "batch");
////                    List<Map<String, Object>> bySql2 = handlerService.findBySql("SELECT version from gq_invbalances where itemnum='" + matcode + "' and storenum='" + warehouse + "' and batchnum='" + batch + "'");
////                    if (CollectionUtils.isNotEmpty(bySql2)){
////                        Map<String, Object> map2 = bySql2.get(0);
////                        String version = MapUtils.getString(map2, "version");
////                        String versions = MapUtils.getString(data, "versions");
////                        if (!version.equals(versions)){
////                            throw new ServiceException("库存可用数量已发生改变请重新选择物料！");
////                        }
////                    }
////                }
////            }
//        }
//        super.beforSave(app, objName, data, isUpdate);
//    }

    @Override
    public void afterSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
        if ("scraplicationApp".equals(app)) {
            if ("gq_Listscrapped".equals(objName)) {
                String scraption = MapUtils.getString(data, "scraption");
                List<Map<String, Object>> bySql1 = handlerService.findBySql("select totalex from gq_scraplication where scraption ='" + scraption + "'");
                Map<String, Object> map1 = bySql1.get(0);
                Float totalex = MapUtils.getFloat(map1, "totalex");
                totalex = (float) 0;
                List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_Listscrapped where scraption ='" + scraption + "'");
                if (CollectionUtils.isNotEmpty(bySql)) {
                    for (int i = 0; i < bySql.size(); i++) {
                        Map<String, Object> map = bySql.get(i);
                        Float linecost = MapUtils.getFloat(map, "linecost");
                        totalex = totalex + linecost;
                        String id = MapUtils.getString(map, "id");
                        String matcode = MapUtils.getString(map, "matcode");
                        List<Map<String, Object>> bySql2 = handlerService.findBySql("select model from gq_item where itemnum ='" + matcode + "'");
                        if (CollectionUtils.isNotEmpty(bySql2)) {
                            Map<String, Object> map2 = bySql2.get(0);
                            String model = MapUtils.getString(map2, "model");
                            handlerService.updateBySql("update gq_Listscrapped set Spmodel ='" + model + "' where id='" + id + "'");
                        }
                    }
                    if (totalex == null) {
                        totalex = (float) 0;
                    } else {
                        handlerService.updateBySql("update gq_scraplication set totalex =" + totalex + " where scraption='" + scraption + "'");

                    }
                }

            }
        }
        super.afterSave(app, objName, data, isUpdate);
    }

//    Override  @
//    public void beforSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
//        if ("scraplicationApp".equals(app)){
//            if ("gq_Listscrapped".equals(objName)) {
//                if (isUpdate == false) {
//                String scraption = MapUtils.getString(data, "scraption");
//                List<Map<String, Object>> bySql1 = handlerService.findBySql("select * from gq_Listscrapped where scraption='" + scraption + "'");
//                if (CollectionUtils.isNotEmpty(bySql1)) {
//                    for (int i = 0; i < bySql1.size(); i++) {
//                        Map<String, Object> map = bySql1.get(i);
//                        String id = MapUtils.getString(map, "id");
//                        String matcode = MapUtils.getString(map, "matcode");
//                        List<Map<String, Object>> bySql = handlerService.findBySql("select model from gq_item where itemnum ='" + matcode + "'");
//                        if (CollectionUtils.isNotEmpty(bySql)) {
//                            Map<String, Object> map1 = bySql.get(0);
//                            String model = MapUtils.getString(map1, "model");
//                            handlerService.updateBySql("update gq_Listscrapped set Spmodel ='" + model + "' where id='" + id + "'");
//                        }
//                    }
//                }
//
//            }
//            }
//        }
//        super.beforSave(app, objName, data, isUpdate);
//    }

    @Override
    public void afterExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op, String event) throws Exception {
        if (flowNode.getId().equals("approvaler")){
            boolean flag = op.isFlag();
            if (flag){
                List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_scraplication where id =" + id);
                Map<String, Object> map = bySql.get(0);
                String scraption = MapUtils.getString(map, "scraption");
                String description = MapUtils.getString(map, "description");
                String status = MapUtils.getString(map, "status");
                String warehouse = MapUtils.getString(map, "warehouse");
                String Wname = MapUtils.getString(map, "Wname");
                String recipient = MapUtils.getString(map, "recipient");
                String wname = MapUtils.getString(map, "Wname");
                String orgcode = MapUtils.getString(map, "orgcode");
                String createuserdesc = MapUtils.getString(map, "createuserdesc");
                String createtime = MapUtils.getString(map, "createtime");
                String deptcode = MapUtils.getString(map, "deptcode");
                Float totalex = MapUtils.getFloat(map, "totalex");
                String reason = MapUtils.getString(map, "reason");
                String subjectno = MapUtils.getString(map, "subjectno");
                Map<String, Object> instoMap = new HashMap<>();
                handlerService.addBizNum("invuseApp", "gq_invuse", instoMap);
                // 查询入库编码
                String invusenum = (String) instoMap.get("invusenum");
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("invusenum",invusenum);
                hashMap.put("description",description);
                hashMap.put("status","审批通过");
                hashMap.put("locations",warehouse);
                hashMap.put("locationsdesc",wname);
                hashMap.put("orgcode",orgcode);
                hashMap.put("deptcode",deptcode);
                hashMap.put("createuser",createuserdesc);
                hashMap.put("createtime",createtime);
                hashMap.put("Istype","报废");
                handlerService.insert("gq_invuse",hashMap);
                List<Map<String, Object>> bySql1 = handlerService.findBySql("select * from gq_Listscrapped where scraption ='" + scraption + "'");
                if (CollectionUtils.isNotEmpty(bySql1)){
                    for (int j=0;j<bySql1.size();j++){
                        Map<String, Object> map1 = bySql1.get(j);
                        String matcode = MapUtils.getString(map1, "matcode");//物资编码
                        String matname = MapUtils.getString(map1, "matname");//物资名称
                        String location = MapUtils.getString(map1, "location");//库位
                        Float inquantity = MapUtils.getFloat(map1, "Inquantity");//库存数量
                        String batch = MapUtils.getString(map1, "batch");//批次
                        Float cost1 = MapUtils.getFloat(map1, "cost");//成本
                        BigDecimal cost = new BigDecimal(cost1);
                        String ordering = MapUtils.getString(map1, "ordering");
                        String createuser = MapUtils.getString(map1, "createuser");
                        //String deptcode1 = MapUtils.getString(map1, "deptcode");
                        // Float linecost1 = MapUtils.getFloat(map1, "linecost");
                        Float quantity = MapUtils.getFloat(map1, "quantity");//报废数量
                        String taskid = MapUtils.getString(map1, "taskid");//任务名称
                        String proid = MapUtils.getString(map1, "proid");//项目名称
                        String scraptime = MapUtils.getString(map1, "scraptime");//报废时间
                        HashMap<String, Object> hashMap1 = new HashMap<>();
                        hashMap1.put("invusenum",invusenum);
                        hashMap1.put("itemnum",matcode);
                        hashMap1.put("locations",warehouse);
                        hashMap1.put("binnum",location);
                        hashMap1.put("itemdesc",matname);
                        hashMap1.put("lotnum",batch);
                        hashMap1.put("Inquantity",inquantity);
                        hashMap1.put("quantity",quantity);
                        hashMap1.put("unitcost",cost);
                        hashMap1.put("taskid",taskid);
                        hashMap1.put("proid",proid);
                        hashMap1.put("actualdate",scraptime);
                        BigDecimal bigDecimal = new BigDecimal(quantity);
                        BigDecimal linecost = new BigDecimal('0');
                         linecost = bigDecimal.multiply(cost);
                        hashMap1.put("linecost",linecost);
                        handlerService.insert("gq_invuseline",hashMap1);
                        //更改库存余量
                        List<Map<String, Object>> bySql2 = handlerService.findBySql("select avblbalance,curbal from gq_invbalances where itemnum='" + matcode + "'and storenum ='" + warehouse +  "' and batchnum='" + batch +  "' and binnum='"+location+"'");
                        if (CollectionUtils.isNotEmpty(bySql2)){
                            for (int i=0;i<bySql2.size();i++){
                                Map<String, Object> map2 = bySql2.get(i);
                                Float avblbalance = MapUtils.getFloat(map2, "avblbalance");
                                Float curbal = MapUtils.getFloat(map2, "curbal");
                                avblbalance = avblbalance - quantity;
                                curbal = curbal-quantity;
                                handlerService.updateBySql("update gq_invbalances set avblbalance ="+avblbalance+",curbal ="+curbal+" where itemnum='" + matcode + "'and storenum ='" + warehouse +  "' and batchnum='" + batch + "' and binnum='"+location+"'");
                            }
                        }
                        float avblbalance =0;
                        float curbal =0;
                        //累加
                        List<Map<String, Object>> bySql3 = handlerService.findBySql("select avblbalance,curbal from gq_invbalances where itemnum='" + matcode + "'and storenum ='" + warehouse + "'");
                        if (CollectionUtils.isNotEmpty(bySql3)){
                            for (int x=0;x<bySql3.size();x++){
                                Map<String, Object> map2 = bySql3.get(x);
                                Float avblbalance1 = MapUtils.getFloat(map2, "avblbalance");
                                Float curbal1 = MapUtils.getFloat(map2, "curbal");
                                avblbalance = avblbalance+avblbalance1;
                                curbal=curbal+curbal1;
                            }
                            handlerService.updateBySql("update gq_inventory set curbaltotal ="+curbal+",avblbalance ="+avblbalance+" where  itemnum='" + matcode + "'and storenum ='" + warehouse + "'");
                        }
                        //存到出库交易表
//                        HashMap<String, Object> hashMap2 = new HashMap<>();
//                        hashMap2.put("invusenum",invusenum);
//                        hashMap2.put("invusedesc",description);
//                        hashMap2.put("itemnum",matcode);
//                        hashMap2.put("itemdesc",matname);
//                        hashMap2.put("storenum",warehouse);
//                        hashMap2.put("quantity",quantity);
//                        hashMap2.put("type","出库");
//                        handlerService.insert("gq_matusetrans",hashMap2);
                        //存到报废交易表
                        HashMap<String, Object> hashMap3 = new HashMap<>();
                        String outonumNew="";//出库编号
                        List<Map<String, Object>> bySql9 = handlerService.findBySql("SELECT instonum from gq_matusetrans where instonum  LIKE '%CDFO%' order by instonum desc limit 1");
                        if(CollectionUtils.isNotEmpty(bySql9)){
                            Map<String, Object> map2 = bySql9.get(0);
                            String instonum = MapUtils.getString(map2, "instonum");
                            int integer = Integer.valueOf(instonum.substring(instonum.length() - 8));
                            integer++;
                            outonumNew="CDFO"+integer;
                        }else{
                            outonumNew="CDFO10000001";
                        }
                        Date date = new Date();
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
                        hashMap3.put("instonum",outonumNew);
                        hashMap3.put("instodesc",description);
                        hashMap3.put("binnum",location);
                        hashMap3.put("subjectno",subjectno);
                        hashMap3.put("itemnum",matcode);
                        hashMap3.put("receiptnum",scraption);
                        hashMap3.put("itemdesc",matname);
                        hashMap3.put("storenum",warehouse);
                        hashMap3.put("storedesc",wname);
                        hashMap3.put("lotnum",batch);
                        hashMap3.put("quantity",quantity);
                        hashMap3.put("taskid",taskid);
                        hashMap3.put("proid",proid);
                        hashMap3.put("unitcost",cost);
                        hashMap3.put("linecost",linecost);
                        hashMap3.put("createtime",simpleDateFormat.format(date));
                        hashMap3.put("createuser",createuser);
                        hashMap3.put("deptcode",deptcode);
                        if (StringUtils.isNotEmpty(recipient)){
                            hashMap3.put("sublessordesc",recipient);
                        }
                        hashMap3.put("unit",ordering);
                        hashMap3.put("type","报废出库");
                        handlerService.insert("gq_matusetrans",hashMap3);
//                        List<Map<String, Object>> bySql4 = handlerService.findBySql("select avblbalance,version from gq_invbalances where itemnum='" + matcode + "'and storenum ='" + warehouse + "' and batchnum='" + batch + "' ");
//                        if (CollectionUtils.isNotEmpty(bySql4)){
//                            Map<String, Object> map2 = bySql4.get(0);
//                            Float avblbalance1 = MapUtils.getFloat(map2, "avblbalance");
//                            Integer version = MapUtils.getInteger(map2, "version");
//                            version=version+1;
//                            //handlerService.updateBySql("update gq_Listscrapped set Inquantity ="+avblbalance1+" where matcode='" + matcode + "' and batch='" + batch + "'");
//                            handlerService.updateBySql("update gq_invbalances set version="+version+" where itemnum='" + matcode + "'and storenum ='" + warehouse + "' and batchnum='" + batch + "'");
//                        }

                    }
                }
                //生成报废入库记录
                List<Map<String, Object>> bySql4 = handlerService.findBySql("select * from gq_Listscrapped where scraption ='" + scraption + "'");
                if (CollectionUtils.isNotEmpty(bySql4)){
                    for (int j=0;j<bySql4.size();j++){
                        Map<String, Object> map1 = bySql4.get(j);
                        String matcode = MapUtils.getString(map1, "matcode");//物资编码
                        String id1 = MapUtils.getString(map1, "id");//报废明细行id;
                        String matname = MapUtils.getString(map1, "matname");//物资名称
                        String location = MapUtils.getString(map1, "location");//库位
                        Float inquantity = MapUtils.getFloat(map1, "Inquantity");//库存数量
                        String batch = MapUtils.getString(map1, "batch");//批次
                        Float cost1 = MapUtils.getFloat(map1, "cost");//成本
                        BigDecimal cost = new BigDecimal(cost1);
                        String linecost1 = MapUtils.getString(map1, "linecost");
                        BigDecimal linecost = new BigDecimal(linecost1);
                        String ordering = MapUtils.getString(map1, "ordering");
                        String spmodel = MapUtils.getString(map1, "Spmodel");
                        String createuser = MapUtils.getString(map1, "createuser");
                        //String deptcode1 = MapUtils.getString(map1, "deptcode");
                        // Float linecost1 = MapUtils.getFloat(map1, "linecost");
                        Float quantity = MapUtils.getFloat(map1, "quantity");//报废数量
                        String taskid = MapUtils.getString(map1, "taskid");//任务名称
                        String proid = MapUtils.getString(map1, "proid");//项目名称
                        String scraptime = MapUtils.getString(map1, "scraptime");//报废时间
                        String schouse="是";
                        String status1="已批准";
                        List<Map<String, Object>> bySql2 = handlerService.findBySql("select * from gq_store where schouse='" + schouse + "' and status='"+status1+"'");
                        if (CollectionUtils.isNotEmpty(bySql2)){
                            String storenum = MapUtils.getString(bySql2.get(0), "storenum");//报废库房编号
                            String storedesc = MapUtils.getString(bySql2.get(0), "storedesc");
                            String shelfnum="";
                            List<Map<String, Object>> bySql7 = handlerService.findBySql("select shelfnum from gq_areashelf where storenum='" + storenum + "'");
                            if (CollectionUtils.isNotEmpty(bySql7)){
                                shelfnum = MapUtils.getString(bySql7.get(0), "shelfnum");//查询报废库货位
                            }

                            List<Map<String, Object>> bySql3 = handlerService.findBySql("select * from gq_item where itemnum ='" + matcode + "'");
                            if (CollectionUtils.isNotEmpty(bySql3)) {
                                Map<String, Object> map2 = bySql3.get(0);
                                Float standardcost = MapUtils.getFloat(map2, "standardcost");//标准成本
                                String packform = MapUtils.getString(map2, "packform");//封装形式
                                String maintechtors = MapUtils.getString(map2, "maintechtors");//主要技术指标
                                String qualitygrade = MapUtils.getString(map2, "qualitygrade");//质量等级
                                String lifemoney = MapUtils.getString(map2, "lifemoney");//寿命
                                String specifications = MapUtils.getString(map2, "specifications");//规格
                                String spemodel = MapUtils.getString(map2, "spemodel");//品牌
                                String manufacturer = MapUtils.getString(map2, "manufacturer");


                                //库存成本表，
                                List<Map<String, Object>> bySql5=null;
                                if (taskid==null||taskid==""){
                                    bySql5 = handlerService.findBySql("select avgcost from gq_invcost where itemnum='" + matcode + "'and storenum='" + storenum + "' and batchnum ='" + batch + "' and proid='" + proid + "' and subjectno='" + subjectno + "'");

                                }else {
                                    bySql5 = handlerService.findBySql("select avgcost from gq_invcost where itemnum='" + matcode + "'and storenum='" + storenum + "' and batchnum ='" + batch + "' and taskid ='" + taskid + "' and proid='" + proid + "' and subjectno='" + subjectno + "'");

                                }
                                if (CollectionUtils.isNotEmpty(bySql5)) {
                                    //查询对应的库存余量表，获取余量
                                    List<Map<String, Object>> bySql6=null;
                                    if (taskid==null||taskid==""){
                                        bySql6= handlerService.findBySql("select * from gq_invbalances where itemnum='" + matcode + "'and storenum='" + storenum + "' and batchnum ='" + batch + "' and proid='" + proid + "' and binnum ='" + shelfnum + "' and subjectno='" + subjectno + "'");

                                    }else {
                                        bySql6= handlerService.findBySql("select * from gq_invbalances where itemnum='" + matcode + "'and storenum='" + storenum + "' and batchnum ='" + batch + "' and taskid ='" + taskid + "' and proid='" + proid + "' and binnum ='" + shelfnum + "' and subjectno='" + subjectno + "'");

                                    }
                                    if (CollectionUtils.isNotEmpty(bySql6)) {
                                        BigDecimal zong = new BigDecimal(0);
                                        BigDecimal avg = new BigDecimal(0);
                                        BigDecimal avgcost = new BigDecimal(0);
                                        for (Map<String, Object> mapl : bySql6) {
                                            String curbal = MapUtils.getString(mapl, "curbal");
                                            BigDecimal bigDecimal = new BigDecimal(curbal);
                                            zong=zong.add(bigDecimal);
                                        }
                                        Map<String, Object> map3 = bySql5.get(0);
                                        Float avgcost1 = MapUtils.getFloat(map3, "avgcost");
                                        BigDecimal bigDecimal = new BigDecimal(avgcost1);
                                        avg = bigDecimal;//库存平均价
                                        BigDecimal bigDecimal1 = new BigDecimal(quantity);
                                            avgcost=((bigDecimal1.multiply(cost)).add(zong.multiply(avg))).divide(bigDecimal1.add(zong));
                                            //avgcost = (innum * unitcost + zong * avg) / (innum + zong); //总的平均价格
                                        if (taskid==null||taskid==""){
                                            handlerService.updateBySql("update gq_invcost set avgcost= '" + avgcost + "',lastcost = '" + cost + "' where itemnum='" + matcode + "'and storenum='" + storenum + "' and batchnum ='" + batch + "' and proid='" + proid + "' and subjectno='" + subjectno + "'");

                                        }else {
                                            handlerService.updateBySql("update gq_invcost set avgcost= '" + avgcost + "',lastcost = '" + cost + "' where itemnum='" + matcode + "'and storenum='" + storenum + "' and batchnum ='" + batch + "' and taskid ='" + taskid + "' and proid='" + proid + "' and subjectno='" + subjectno + "'");

                                        }
                                    }
                                }else {
                                    HashMap<String, Object> hashMap3 = new HashMap<>();
                                    hashMap3.put("batchnum", batch);
                                    hashMap3.put("taskid", taskid);
                                    hashMap3.put("proid", proid);
                                    hashMap3.put("subjectno", subjectno);
                                    hashMap3.put("avgcost", cost);
                                    hashMap3.put("itemnum", matcode);
                                    hashMap3.put("itemdesc", matname);
                                    hashMap3.put("storenum", storenum);
                                    hashMap3.put("stdcost", standardcost);
                                    hashMap3.put("createuser", createuser);//创建人
                                    hashMap3.put("createuserdesc", createuserdesc);//创建人描述
                                    hashMap3.put("deptcode", deptcode);//创建人描述
                                    handlerService.insert("gq_invcost", hashMap3);
                                }
                                //更新库存
                                Map<String, Object> mapTory = new HashMap<>();
                                mapTory.put("itemnum", matcode);//物料编码
                                mapTory.put("itemdesc", matname);////物料名称
                               // mapTory.put("itemtype", linetype);//物资类型
                                mapTory.put("storenum", storenum);//仓库编号
                                mapTory.put("storedesc", storedesc);//库房名称
                                mapTory.put("moneylife", lifemoney);//寿命
                                mapTory.put("model", spmodel);//规格型号
                                mapTory.put("specifications",specifications);//规格
                                mapTory.put("spemodel", spemodel);//品牌
                                mapTory.put("manufacturer", manufacturer);//生产厂家
                                mapTory.put("packagform", packform);//封装形式
                                mapTory.put("orderunit", ordering);//订购单位
                                mapTory.put("maintechindicat", maintechtors);//主要技术指标
                                mapTory.put("quagrade", qualitygrade);//质量等级
                                mapTory.put("curbaltotal", quantity);//库存余量
                                mapTory.put("avblbalance", quantity);//可用余量
                                mapTory.put("createuser", createuser);//创建人
                                mapTory.put("createuserdesc", createuserdesc);//创建人描述
                                mapTory.put("deptcode", deptcode);//创建人描述
                                //插入库存余量
                                HashMap<String, Object> hashMap7 = new HashMap<>();
                                hashMap7.put("itemnum", matcode);
                                hashMap7.put("storenum", storenum);
                                hashMap7.put("itemdesc", matname);
                                hashMap7.put("unitcost", cost);
                                hashMap7.put("binnum", shelfnum);
                                hashMap7.put("batchnum", batch);
                                hashMap7.put("taskid", taskid);
                                hashMap7.put("proid", proid);
                                hashMap7.put("orderunit",ordering);
                                hashMap7.put("subjectno", subjectno);
                                hashMap7.put("receiptnum", "0");
                                hashMap7.put("avblbalance", quantity);
                                hashMap7.put("curbal", quantity);
                                String startdate =  DateUtiles.getYmdhms(new Date());
                                hashMap7.put("startdate",startdate);//入库时间
//                                hashMap.put("enddate", life);
                                hashMap7.put("createuser", createuser);//创建人
                                hashMap7.put("createuserdesc", createuserdesc);//创建人描述
                                hashMap7.put("deptcode", deptcode);//创建人描述
                                hashMap7.put("fromstorenum",warehouse);//报废前库房编号
                                hashMap7.put("fromstoredesc",Wname);//报废前库房名称
                                //查询库存主表，获取库存余量表 库 余量表这条数据查询，
                                List<Map<String, Object>> bySql8=null;
                                if (taskid==null||taskid==""){
                                    bySql8 = handlerService.findBySql("select * from gq_invbalances where itemnum ='" + matcode + "' and storenum ='" + storenum + "' and binnum ='" + shelfnum + "' and batchnum ='" + batch + "' and proid ='" + proid + "' and subjectno='" + subjectno + "'");

                                }else {
                                    bySql8 = handlerService.findBySql("select * from gq_invbalances where itemnum ='" + matcode + "' and storenum ='" + storenum + "' and binnum ='" + shelfnum + "' and batchnum ='" + batch + "' and taskid ='" + taskid + "' and proid ='" + proid + "' and subjectno='" + subjectno + "'");

                                }
                                if (CollectionUtils.isNotEmpty(bySql8)) {
                                    //更新库存余量表
                                    Map<String, Object> map3 = bySql8.get(0);
                                    Float curbal = MapUtils.getFloat(map3, "curbal"); //当前余量
                                    //Map<String, Object> map4 = bySql4.get(0);
                                    Float avblbalance = MapUtils.getFloat(map3, "avblbalance");//可用余量
                                        curbal = curbal + quantity;
                                        avblbalance = avblbalance + quantity;

                                    if (taskid==null||taskid==""){
                                        handlerService.updateBySql("update gq_invbalances set curbal = '" + curbal + "',avblbalance = '" + avblbalance + "' where itemnum ='" + matcode + "' and storenum ='" + storenum + "' and binnum ='" + shelfnum + "' and batchnum ='" + batch + "' and proid ='" + proid + "' and subjectno='" + subjectno + "'");

                                    }else {
                                        handlerService.updateBySql("update gq_invbalances set curbal = '" + curbal + "',avblbalance = '" + avblbalance + "' where itemnum ='" + matcode + "' and storenum ='" + storenum + "' and binnum ='" + shelfnum + "' and batchnum ='" + batch + "' and taskid ='" + taskid + "' and proid ='" + proid + "' and subjectno='" + subjectno + "'");

                                    }
                                }else {
                                    //新增库存余量表信息
                                    handlerService.insert("gq_invbalances", hashMap7);
                                }
                                //查询库存主表信息
                                List<Map<String, Object>> bySql9 = handlerService.findBySql("select * from gq_inventory where  itemnum  ='" + matcode + "'and storenum ='" + storenum + "'");
                                    if (CollectionUtils.isNotEmpty(bySql9)){
                                        List<Map<String, Object>> bySql6 = handlerService.findBySql("select * from gq_invbalances where  itemnum  ='" + matcode + "'and storenum ='" + storenum + "'");
                                        float curbal = 0;//库存余量
                                        float avblbalance = 0;//可用余量
                                        if (CollectionUtils.isNotEmpty(bySql6)){
                                            for (Map<String, Object> objectMap : bySql6) {
                                                Float curbal1 = MapUtils.getFloat(objectMap, "curbal");
                                                curbal = curbal + curbal1;//库存余量
                                                Float avblbalance1 = MapUtils.getFloat(objectMap, "avblbalance");
                                                avblbalance = avblbalance + avblbalance1;//库存可用余量
                                            }
                                            handlerService.updateBySql("update gq_inventory set curbaltotal = " + curbal + ", avblbalance = " + avblbalance + " where storenum = '" + storenum + "' and itemnum = '" + matcode + "'");

                                        }

                                    } else {
                                        handlerService.insert("gq_inventory", mapTory);
                                    }
                                    String instonumNew="";//入库编号
                                List<Map<String, Object>> bySql10 = handlerService.findBySql("SELECT instonum from gq_matusetrans where instonum  LIKE '%CDFI%' order by instonum desc limit 1");
                                if (CollectionUtils.isNotEmpty(bySql10)){
                                    Map<String, Object> map7 = bySql10.get(0);
                                    String instonum1 = MapUtils.getString(map7, "instonum");
                                    int integer = Integer.valueOf(instonum1.substring(instonum1.length() - 8));
                                    integer++;
                                    instonumNew="CDFI"+integer;
                                }else{
                                    instonumNew="CDFI10000001";
                                }
                                handlerService.updateBySql("insert into gq_matusetrans (subjectno,deptcode,instonum,instodesc,itemnum,itemdesc,quantity,unit,createtime,createuser,createuserdesc,storenum,storedesc,lotnum,type,unitcost,linecost,proid,taskid,binnum,status) values('"+subjectno+"','"+deptcode+"','"+instonumNew+"','"+description+"','"+matcode+"','"+matname+"',"+quantity+",'"+ordering+"',now(),'"+createuser+"','"+createuserdesc+"','"+storenum+"','"+storedesc+"','"+batch+"','报废',"+cost+","+linecost+",'"+proid+"','"+taskid+"','"+shelfnum+"','"+status+"')");

                            }
                            }

                            }
                        }
                    }
        }
        super.afterExcuteFlow(app, id, flowNode, op, event);
    }
}
